# encoding: utf-8
import threading

import time

from com import CTS, MyDevice, ScriptJava, ScriptPy, DeviceHelper, LogUtil
from flash_tool.flash import FlashTools, specify_rom_task

total_count = 6
# cts目录
android_cts_dir = '/home/crystalchen/DEV/My_CTS/V6/android-cts'
# media文件目录
cts_media_dir = '/home/crystalchen/DEV/My_CTS/android-cts-media-1.3'
# 测试报告的路径
cts_report_dir = '/home/crystalchen/DEV/My_CTS/Report'
# 设备的序列号
dev_list = ['cf47caad', 'cf47ca02']  # 'c4b5c29f','c5c3c2e2','cf47ca02','cf47caad' c5c3c2e2 ,cf47caad
rom_dir = r'\\172.16.1.230\Tiffany\3.dailybuild_release\10.MEIOS_T9_DVT2_DailyBuild_8.1\SDM660O_MEIOS_DVT2_Tiffany_180404_1129_r31416_user_sign_efuse'

'''运行cts'''


def normal_run(cts, dev_list=dev_list):
    assert isinstance(cts, CTS)


    while cts.roundNum < total_count:
        LogUtil.i( '\n\n--------第【 ' + str(cts.roundNum) + ' 】轮测试-------\n')
        # 部署
        tasks = []
        for num in dev_list:
            cts_pre_taks = PrepareTask(num)
            tasks.append(cts_pre_taks)
            cts_pre_taks.start()
        for t in tasks:
            t.join()

        # 本轮用到的命令
        run_plan = cts.get_commands()
        LogUtil.i(run_plan)
        # cts.run_cts(run_plan)
        cts.show_result()
        cts.roundNum += 1


'''刷机并恢复出厂设置'''


def flash_rom():
    # 获取指定版本
    local_dir = FlashTools.get_specify_rom(rom_dir, unzip_dir='./cts_rom/')
    LogUtil.i('Target rom local dir:'+local_dir)
    # local_dir = './cts_rom/Tiffany_user_fastboot/'
    if local_dir is None:
        LogUtil.e('flash mission abortted!')
    mtasks = []
    for dev_num in dev_list:
        t = FlashTask(dev_num, local_dir, save_userdata=False, recovery=True)
        mtasks.append(t)
        t.start()
    for t in mtasks:
        t.join()

    LogUtil.i('cts 测试机刷机完毕！')


class FlashTask(threading.Thread):  # 继承父类threading.Thread
    def __init__(self, dev_num, local_rom, save_userdata=False, recovery=True):
        threading.Thread.__init__(self)
        self.mdev = MyDevice(dev_num)
        self.local_rom = local_rom
        self.save_userdata = save_userdata
        self.recovery = recovery

    def run(self):
        self.mdev.wait_device_ready()
        flash_ok = FlashTools.flash_by_py(self.mdev.dev_num, self.local_rom, self.save_userdata)
        if flash_ok:
            self.mdev.i('刷机完成！')
            DeviceHelper(self.mdev.dev_num).wait_device_ready()
            time.sleep(5)
            if self.recovery:
                script = ScriptJava(self.mdev)
                script.recovery_all()
                time.sleep(10)
            DeviceHelper(self.mdev.dev_num).wait_device_ready()
            time.sleep(10)
        else:
            self.mdev.i('刷机失败！')


class PrepareTask(threading.Thread):
    def __init__(self, dev_num):
        threading.Thread.__init__(self)
        self.script = ScriptJava(MyDevice(dev_num))

    def run(self):
        self.script.for_cts_prepare()


if __name__ == '__main__':
    flash_rom()
    cts = CTS(android_cts_dir, cts_media_dir, cts_report_dir, dev_list)
    normal_run(cts, dev_list)

    # mdev = MyDevice('c5c3c2e2')
    # mdev.wait_device_ready()
    # script_py = ScriptJava(mdev)
    # script_py.for_cts_prepare()
    #
    # for num in dev_list:
    #     d=DeviceHelper(num)
    #     d.wait_device_ready()
